//
//  AddressView.m
//  XDBaseFrameWork
//
//  Created by xindao on 14-9-30.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "AddressView.h"

@implementation AddressView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self makeViews];
    }
    return self;
}
-(void)makeViews
{
    self.provinceNumber = 0;
    self.cityNumber = 0;
    self.areaNumber = 0;

    self.bgView = [[UIView alloc] initWithFrame:self.bounds];
    _bgView.backgroundColor = [UIColor blackColor];
    _bgView.alpha = 0.3;

    self.pickeBgView  = [UIView initViewRect:CGRectMake(0, self.frame.size.height-492/2.0f, UI_SCREEN_WIDTH, 492/2.0f) andBackGroudColor:[UIColor whiteColor]];

    self.titleLB = [UILabel initLabelText:@"请选择预约时间" andRect:CGRectMake(0,0,UI_SCREEN_WIDTH,33) andBackGroudColor:mClearColor andTextFont:[UIFont systemFontOfSize:17] andTextColor:UIColorFromRGB(0xff0e03) andTextAlignment:NSTextAlignmentCenter];

    self.pickerView  = [[UIPickerView alloc] initWithFrame:CGRectMake(0,33, 320,190)];
    _pickerView.showsSelectionIndicator = YES;
    _pickerView.delegate = self;
    _pickerView.dataSource =self;
    _pickerView.backgroundColor = [UIColor whiteColor];

    self.sureBtn = [UIButton initButton:CGRectMake((320-115)/2.0f, self.pickeBgView.frame.size.height-40, 115, 30) btnNorImage:LOADIMAGE(@"timesure@2x") btnDisableBtn:LOADIMAGE(@"timesure_dis@2x") btnTitle:@"确定" titleNorColor:UIColorFromRGB(0xffffff) titleSelectColor:UIColorFromRGB(0xffffff) titleFont:[UIFont systemFontOfSize:15]];
    [_sureBtn setTitle:@"已占用" forState:UIControlStateDisabled];
    [_sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgViewClick)];
    [_bgView addGestureRecognizer:tap];

    [self addSubview:_bgView];
    [self addSubview:_pickeBgView];

    [self.pickeBgView addSubview:_titleLB];
    [self.pickeBgView addSubview:_pickerView];
    [self.pickeBgView addSubview:_sureBtn];

    self.provinceArray = [XDTools getArrayWithKey:nil table:@"provience"];
    [_pickerView reloadComponent:0];
    self.cityArray = [XDTools getArrayWithKey:_provinceArray[0] table:@"city"];
    [_pickerView reloadComponent:1];
    self.areaArray = [XDTools getArrayWithKey:_cityArray[0] table:@"area"];
    [_pickerView reloadComponent:2];
}




#pragma mark UIPickeView Delegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return 1;
            break;
        case 1:
            return [_cityArray count];
            break;
        case 2:
            return [_areaArray count];
            break;
        default:
            break;
    }
    return 1;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (component) {
        case 0:
        {
            return [_provinceArray objectAtIndex:row];
        }
            break;
        case 1:
        {
            return [_cityArray objectAtIndex:row];
        }
            break;
        case 2:
        {
            return [_areaArray objectAtIndex:row];
        }
            break;
        default:
            break;
    }
    return nil;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            self.provinceNumber = row;
            self.cityArray = [XDTools getArrayWithKey:_provinceArray[row] table:@"city"];
            [_pickerView reloadComponent:1];
            self.areaArray = [XDTools getArrayWithKey:_cityArray[0] table:@"area"];
            [_pickerView reloadComponent:2];
            break;
        case 1:
            self.cityNumber = row;
            self.areaArray = [XDTools getArrayWithKey:_cityArray[row] table:@"area"];
            [_pickerView reloadComponent:2];
            break;
        case 2:
            self.areaNumber = row;
            break;
        default:
            break;
    }

}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
//    if (component == 0){
        return 320/3.0f;
//    }else if (component == 1){
//        return 90;
//    }else{
//        return 90;
//    }
}

-(void)bgViewClick
{
    self.hidden = YES;
}

-(void)sureBtnClick
{
    self.hidden = YES;
    if (_provinceNumber > _provinceArray.count-1) {
        _provinceNumber = _provinceArray.count-1;
    }
    if (_cityNumber > _cityArray.count-1) {
        _cityNumber = _cityArray.count-1;
    }
    if (_areaNumber > _areaArray.count-1) {
        _areaNumber = _areaArray.count-1;
    }
    [_delegate chooseAddressWithProvince:[self.provinceArray objectAtIndex:self.provinceNumber] city:[self.cityArray objectAtIndex:self.cityNumber] area:[self.areaArray objectAtIndex:self.areaNumber]];
}

-(void)setTheViewHidden
{
    self.hidden = YES;
}

-(void)setTheViewShow
{
    self.hidden = NO;
}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
